<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>formData</title>
</head>
<body>
    <!-- <form action="/upload" method="POST" enctype="multipart/form-data">
        <input type="text" name="name">
        <input type="text" name="pwd">
        <button type="submit">提交</button>
    </form> -->
    <input type="file" id="file" >
    <script>

        document.getElementById('file').onchange = function () {
            console.log(
                this.files
            )
        }

        let f = new FormData();
        f.append('name', '123')
        f.append('pwd', '456')
        f.append('file', this.files)

        let ajax = function(options) {
            var xhr = new XMLHttpRequest();
            
            if (options.method.toUpperCase() === 'POST') {
                xhr.open(
                    options.method,
                    options.url,
                    true
                )
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
                // 发送请求
                if (options.type === 'form-data') {
                    xhr.send(options.data)
                } else {
                    xhr.send(encodeParam(options.data))
                }
            } else if (options.method.toUpperCase() === 'GET') {
                xhr.open(
                    options.method,
                    options.url + '?'+encodeParam(param),
                    true
                )
                xhr.send()
            }

            // 接受返回值
            xhr.onreadystatechange = () => {
                if (xhr.readyState === 4) {
                    if (xhr.status >= 200 && xhr.status <= 300 || xhr.status === 304) {
                        let res = xhr.response
                        if (options.dataType === 'json') {
                            res = JSON.parse(res);
                        }
                        options.success && options.success(res)
                    } else {
                        options.error && options.error(xhr.response)
                    }
                }
            }
        }

        ajax({
            url: '/upload',
            data: f,
            method: 'POST',
            type: 'form-data',
            success: function() {

            }
        })

    </script>
</body>
</html>
